//
//  SHPaymentCardViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/7/18.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
//首瀚付款卡页面
#import "BankCardModel.h"
#import "SHBankCarCell.h"
#import "SHAuthFourthStatusPutPhoneController.h"
#import "SHAddPayBankCardController.h"
#import "SHPaymentCardViewController.h"

@interface SHPaymentCardViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *bankCardDataArray;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation SHPaymentCardViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = YES;
    //添加支付卡成功，接受刷新支付卡列表通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPayCardNotificationAction:) name:@"refreshPayCardNotification" object:nil];
    

}

#pragma mark - 添加支付卡成功，重新请求数据
- (void)refreshPayCardNotificationAction:(NSNotification *)notification{
    
    [self requstBankCardInfo:YES];

    
}
- (NSMutableArray *)bankCardDataArray{
    if (!_bankCardDataArray) {
        _bankCardDataArray = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _bankCardDataArray;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        [_tableView registerClass: [SHBankCarCell  class]  forCellReuseIdentifier:@"BankCard"];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self configUI];
    [self requstBankCardInfo:YES];
    [self refresh];
    
    
    
}
- (void)refresh{
    
    __unsafe_unretained __typeof(self) weakSelf = self;
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requstBankCardInfo:YES];
        
        // 马上进入刷新状态
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    
}


#pragma mark -- 初始化UI
- (void)configUI{
    
    self.view.backgroundColor = KBASEBGCOLOR;
    
}

#pragma mark -- 获取银行卡信息
- (void)requstBankCardInfo:(BOOL)isRemove{
    
    
    [SHUserCenterHttp getBankCardLsitWithUsingChoices:2 bankCardType:12 success:^(NSMutableArray *bankCardListArray) {
        if (isRemove) {
            [self.bankCardDataArray removeAllObjects];
            
        }
        for (BankCardModel *model in bankCardListArray) {
            model.using_choices = 2;
            [self.bankCardDataArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            
        });
        
    } complection:^(id result) {
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(id erro) {
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    
}



#pragma mark --- UITablView  & DataSource  & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.bankCardDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SHBankCarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCard"];
    cell.default_useLabel.hidden = YES;
    BankCardModel *model = self.bankCardDataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(60);
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
  
    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        BankCardModel *model = self.bankCardDataArray[indexPath.row];
        if (model.auth_4_status) {//四要素认证通过只能修改手机号
            SHAuthFourthStatusPutPhoneController  *bindingBankCardVc = [[SHAuthFourthStatusPutPhoneController  alloc]init];
            
            if (self.push) {
                self.push(bindingBankCardVc);
            }
        }else{//四要素未认证通过，重新修改认证
            SHAddPayBankCardController  *bindingBankCardVc = [[SHAddPayBankCardController  alloc]init];
            bindingBankCardVc.payCardUsingType = SHPayCardUsingTypePut;
            bindingBankCardVc.paybankCardModel  = model;
            if (self.push) {
                self.push(bindingBankCardVc);
            }
        }
        

        
        
    }];
    editRowAction.backgroundColor = [UIColor orangeColor];//可以定义RowAction的颜色
    UITableViewRowAction *deletRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        BankCardModel *model = self.bankCardDataArray[indexPath.row];
        [self deletePayBankCardWithBankCardModel:model];
        
        
    }];
    deletRowAction.backgroundColor = [UIColor redColor];//可以定义RowAction的颜色
    return @[deletRowAction];//最后返回这俩个RowAction 的数组
    
    
    
    
}



- (void)deletePayBankCardWithBankCardModel:(BankCardModel *)model{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [SHUserCenterHttp deleteBankCardWithCardId:model.bank_card_code success:^(id result) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.bankCardDataArray removeObject:model];
        [self.tableView reloadData];

        
    } complection:^(id result) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(id erro) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];

  }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
